<template>
	<view class="">
		<common-title :title="titleContent" @clickLeft="backToUpPage" @clickRight="editUserBirthday"></common-title>
		<view class="passwd">
			<view class="passwd-item">
				<view class="passwd-item-label">原密码</view><input class="passwd-item-input" v-model="oldPasswd" placeholder="填写原密码" type="password"/>
			</view>
			<view class="passwd-item">
				<view class="passwd-item-label">新密码</view><input class="passwd-item-input" v-model="newPasswd" placeholder="填写新密码" type="password"/>
			</view>
			<view class="passwd-item">
				<view class="passwd-item-label">确认密码</view><input class="passwd-item-input" v-model="againNewPasswd" placeholder="再次填写确认" type="password"/>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref } from "vue";
	import { onLoad } from '@dcloudio/uni-app'
	import { editUserPasswd } from "../../../../utils/apiMine";
	import { encrypt } from "../../../../utils/crypto";
	//================================common-title=========================================
	//标题内容
	const titleContent = ref({
		left:'',
		midst:'修改密码',
		right:'保存'
	});
	
	//返回
	const backToUpPage = () => {
		uni.navigateBack();
	};
	//保存修改
	const editUserBirthday = async () => {
		if (oldPasswd.value.length === 0 || newPasswd.value.length === 0 || againNewPasswd.value.length === 0) {
			uni.showToast({
				title:'请填写完整',
				icon:'error'
			})
			return;
		}
		if (newPasswd.value !== againNewPasswd.value) {
			uni.showToast({
				title:'密码输入不一致',
				icon:'error'
			})
			return;
		}
		await editUserPasswd({
			'oldPasswd':encrypt(oldPasswd.value),
			'newPasswd':encrypt(newPasswd.value)
		}).then((res) => {
			// console.log(res);
			if (res === "200") {
				uni.showToast({
					title:'修改成功'
				})
				uni.navigateBack();
			}
			else if(res === "1006") {
				uni.showToast({
					title:'原密码错误',
					icon:"error"
				})
			}
			else {
				uni.showToast({
					title:'修改失败',
					icon:"error"
				})
			}
		})
	}
	//================================密码逻辑处理=========================================
	const oldPasswd = ref('');
	const newPasswd = ref('');
	const againNewPasswd = ref('');
</script>

<style lang="scss" scoped>
	.passwd {
	    padding-top: 50rpx; // 上边距
	
	    .passwd-item {
	        display: flex; // 使用 Flexbox 布局
	        align-items: center; // 垂直居中对齐标签和输入框
	        padding: 30rpx; // 单个密码项内边距
	        font-size: 32rpx; // 字体大小
	
	        .passwd-item-label {
	            flex: 0 0 160rpx; // 设置标签宽度，确保所有标签对齐，您可以根据需要调整宽度
	            margin-right: 10rpx; // 标签与输入框之间的间距
	            text-align: left; // 左对齐
	        }
	
	        .passwd-item-input {
	            flex-grow: 1; // 使得输入框扩展以填满剩余空间
	            // background-color: #676767; // 背景颜色
	            border: none; // 去掉边框
	            border-bottom: 1px solid #808080;// 下边框
	            padding: 5rpx; // 输入框内边距
	            font-size: 32rpx; // 输入框的字体大小
	        }
	    }
	}
</style>